﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;

using iMainAddin;

namespace iAdministerAddin
{
	public enum eCommandType
	{
		ClassCommand,
		ProjectCommand,
	}
	class iAdministerCommand : iAdminister
	{
		private List<CommandEvents> commandItemHandlerList = new List<CommandEvents>();
		Dictionary<String, iCommand> m_CommandList = new Dictionary<string, iCommand>();
        ~iAdministerCommand()
        {
        }
		public iCommand GetCommand(string commandID)
		{
			iCommand SelectCommand = m_CommandList[commandID];
			return SelectCommand;
		}
		public void AddCommand(iCommand cmd)
		{
            Command menuItem = cmd.GetVSCommand(m_AddInInst);
			AddCommandEventHandler(menuItem);
			AddCommandToList(cmd);
	}
		public void AddCommandEventHandler(Command command)
		{
			// 여기서 부터 명령 시작
			EnvDTE.CommandEvents Event
						= (EnvDTE.CommandEvents)m_Application.DTE.Events.get_CommandEvents(command.Guid, command.ID);
			Event.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(Execute);
			commandItemHandlerList.Add(Event);
		}
		private void AddCommandToList(iCommand cmd)
		{
			if (!m_CommandList.ContainsKey(cmd.Id.ToString()))
			{
				m_CommandList.Add(cmd.Id.ToString(), cmd);
			}
		}

		private void Execute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
		{
			try
			{
				// We perform the command only if we found the command corresponding to the menu item clicked
				Command menuItem = (Command)CustomIn;
				if (m_CommandList.ContainsKey(menuItem.ID.ToString()))
				{
					m_CommandList[menuItem.ID.ToString()].Perform();
				}
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message, "Explorer add in");
			}

		}
	}
}
